import os from 'node:os'
import { defineService } from 'easy'

export default defineService({
  handler(ctx) {
    let memoryUsage = 0
    for (let v of Object.values(process.memoryUsage())) {
      memoryUsage += Number.parseInt(v)
    }
    memoryUsage = memoryUsage / (1024 * 1024)
    const body = {
      header: ctx.headers,
      host: ctx.res.host,
      server_status: {
        memory: {
          totol: os.totalmem() / (1024 * 1024),
          free: os.freemem() / (1024 * 1024),
          usage: memoryUsage,
        },
        cpu: os.cpus(),
        load: os.loadavg(),
      },
      hostname: ctx.res.hostname,
      url: ctx.request.url,
      origin: ctx.res.origin,
      originalUrl: ctx.res.originalUrl,
      queryParams: ctx.query,
      queryLength: ctx.query && ctx.query.c ? ctx.query.c.length : '',
      now: new Date().toUTCString(),
    }
    ctx.send(body)
  },
})
